﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DXApplicationTangche.UC.生产计划.编排生产批.service;
using DXApplicationTangche.UC.订单一览新.订单预览新;
using DXApplicationTangche.UC.项目流程.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.生产计划.生产批生产
{
    public partial class XtraUserControl审版 : DevExpress.XtraEditors.XtraUserControl
    {
        private String mnemonic { get; set; }
        private String empName { get; set; }
        public XtraUserControl审版()
        {
            InitializeComponent();
            this.searchLookUpEdit审版选择员工.Properties.DataSource = 任务Service.getAllStaffs();
        }
        #region 选择员工
        private void searchLookUpEdit审版选择员工_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear审版选择员工Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit审版选择员工View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit审版选择员工.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.mnemonic = this.searchLookUpEdit审版选择员工.Properties.View.GetRowCellValue(rowHandle, "mnemonic").ToString();
                    this.empName = this.searchLookUpEdit审版选择员工.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear审版选择员工Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit审版选择员工.ToolTip = null;
            searchLookUpEdit审版选择员工.EditValue = null;
        }
        private void searchLookUpEdit审版选择员工_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.empName;
        }
        #endregion
        private void textBox审版扫描生产批号_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyValue == 13)
            {
                if (!String.IsNullOrWhiteSpace(this.mnemonic))
                {
                    String batchNo = this.textBox审版扫描生产批号.Text.Trim();
                    //记录审版接受
                    编排生产批Service.saveBatchProjectWorkRecode(this.mnemonic, batchNo, ApsConsts.REVIEW_RECEIVE);
                    try
                    {
                        DTO生产批 dTO生产批 = 编排生产批Service.getWIPBatchByBatchNo(batchNo);
                        if (String.IsNullOrEmpty(dTO生产批.orderId))
                        {
                            return;
                        }
                        //进入审版界面
                        new Form订单预览新(dTO生产批, this.mnemonic).ShowDialog();
                        this.textBox审版扫描生产批号.Clear();
                        this.ActiveControl = this.textBox审版扫描生产批号;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                else
                {
                    MessageBox.Show("请选择员工");
                }
            }
        }

        
    }
}
